package com.ljj.gulimall.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class GulimallCorsConfiguration {

	@Bean
	public CorsWebFilter corsWebFilter() {
		UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();

		CorsConfiguration configuration = new CorsConfiguration();
		// 1、配置跨域信息
		configuration.addAllowedHeader("*");// 配置允许跨域的头（Header）信息
		configuration.addAllowedMethod("*");// 设置允许跨域的方法
		configuration.addAllowedOrigin("*");// 设置允许请求来源为任意请求来源
		configuration.setAllowCredentials(true); // 允许携带cookie进行跨域
		
		configSource.registerCorsConfiguration("/**", configuration);
		return new CorsWebFilter(configSource);
	}
}
